﻿#region Copyright (c) 2006-2012 nHydrate.org, All Rights Reserved
// --------------------------------------------------------------------- *
//                           NHYDRATE.ORG                                *
//              Copyright (c) 2006-2012 All Rights reserved              *
//                                                                       *
//                                                                       *
// This file and its contents are protected by United States and         *
// International copyright laws.  Unauthorized reproduction and/or       *
// distribution of all or any portion of the code contained herein       *
// is strictly prohibited and will result in severe civil and criminal   *
// penalties.  Any violations of this copyright will be prosecuted       *
// to the fullest extent possible under law.                             *
//                                                                       *
// THE SOURCE CODE CONTAINED HEREIN AND IN RELATED FILES IS PROVIDED     *
// TO THE REGISTERED DEVELOPER FOR THE PURPOSES OF EDUCATION AND         *
// TROUBLESHOOTING. UNDER NO CIRCUMSTANCES MAY ANY PORTION OF THE SOURCE *
// CODE BE DISTRIBUTED, DISCLOSED OR OTHERWISE MADE AVAILABLE TO ANY     *
// THIRD PARTY WITHOUT THE EXPRESS WRITTEN CONSENT OF THE NHYDRATE GROUP *
//                                                                       *
// UNDER NO CIRCUMSTANCES MAY THE SOURCE CODE BE USED IN WHOLE OR IN     *
// PART, AS THE BASIS FOR CREATING A PRODUCT THAT PROVIDES THE SAME, OR  *
// SUBSTANTIALLY THE SAME, FUNCTIONALITY AS THIS PRODUCT                 *
//                                                                       *
// THE REGISTERED DEVELOPER ACKNOWLEDGES THAT THIS SOURCE CODE           *
// CONTAINS VALUABLE AND PROPRIETARY TRADE SECRETS OF NHYDRATE,          *
// THE REGISTERED DEVELOPER AGREES TO EXPEND EVERY EFFORT TO             *
// INSURE ITS CONFIDENTIALITY.                                           *
//                                                                       *
// THE END USER LICENSE AGREEMENT (EULA) ACCOMPANYING THE PRODUCT        *
// PERMITS THE REGISTERED DEVELOPER TO REDISTRIBUTE THE PRODUCT IN       *
// EXECUTABLE FORM ONLY IN SUPPORT OF APPLICATIONS WRITTEN USING         *
// THE PRODUCT.  IT DOES NOT PROVIDE ANY RIGHTS REGARDING THE            *
// SOURCE CODE CONTAINED HEREIN.                                         *
//                                                                       *
// THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE.              *
// --------------------------------------------------------------------- *
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using nHydrate.Dsl;
using Microsoft.VisualStudio.Modeling;

namespace nHydrate.DslPackage.Forms
{
	public partial class IndexesForm : Form
	{
		private nHydrateModel _model = null;
		private Store _store = null;
		private List<Entity> _entityList = null;

		public IndexesForm()
		{
			InitializeComponent();

			lvwItem.Columns.Clear();
			lvwItem.Columns.Add("Types");
			lvwItem.Columns.Add("Entity");
			lvwItem.Columns.Add("Columns");
			lvwItem.Resize += new EventHandler(AutoSizeGrid);
			AutoSizeGrid(null, null);
		}

		public IndexesForm(List<Entity> entityList, nHydrateModel model, Store store)
			: this()
		{
			_model = model;
			_store = store;
			_entityList = entityList;
			LoadGrid();
			lblHeader.Text = "A list if indexed fields";
		}

		private void LoadGrid()
		{
			lvwItem.Items.Clear();
			foreach (var entity in _entityList)
			{
				foreach (var index in entity.Indexes)
				{
					var li = new ListViewItem();
					li.Tag = index;

					//Types
					var types = string.Empty;
					if (index.IndexType == IndexTypeConstants.PrimaryKey)
						types += "PK ";
					//else
					//  types += "User ";

					if (index.IsUnique)
					{
						types += "UQ ";
					}

					if (index.Clustered)
					{
						types += "CLUSTERED ";
					}

					li.SubItems[0].Text = types;

					//Entity
					li.SubItems.Add(entity.Name);

					//Columns
					var text = string.Empty;
					foreach (var indexColumn in index.IndexColumns.OrderBy(x => x.SortOrder))
					{
						var field = indexColumn.GetField();
						if (field != null)
							text += field.Name + ",";
						else
							text += "(Undefined Column),";
					}
					if (text.EndsWith(","))
						text = text.Substring(0, text.Length - 1);
					li.SubItems.Add(text);

					lvwItem.Items.Add(li);
				}
			}
		}

		private void cmdClose_Click(object sender, EventArgs e)
		{
			this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.Close();
		}

		private void AutoSizeGrid(object sender, EventArgs e)
		{
			const int COL1 = 120;
			const int COL2 = 160;
			var width = lvwItem.Width - 30;
			lvwItem.Columns[0].Width = COL1;
			lvwItem.Columns[1].Width = COL2;
			lvwItem.Columns[2].Width = width - COL1 - COL2;
		}

		private void ShowColumns()
		{
			if (lvwItem.SelectedItems.Count > 0)
			{
				var index = lvwItem.SelectedItems[0].Tag as Index;
				var F = new IndexColumnOrder(index, _model, _store);
				if (F.ShowDialog() == System.Windows.Forms.DialogResult.OK)
				{
					LoadGrid();
				}
			}
		}

		private void lvwItem_DoubleClick(object sender, EventArgs e)
		{
			ShowColumns();
		}

		private void lvwItem_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
			{
				ShowColumns();
			}
		}

		private void cmdEdit_Click(object sender, EventArgs e)
		{
			ShowColumns();
		}

	}
}
